    $.fn.wait = function(time, type) {
        time = time || 100;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };
		var stepstate=0;//收缩
		var MARKPAD = new Array();
		var KW = new Array();
		var TESTS= new Array();
		var subjectname="";

			

$(function(){
		
		$(".preloader").wait().hide(1000);
		initstep0(0);
		$(".geneva-close-button").click(function(){if(stepstate==0){initstep0(0);}});
		$("#iv-step0 li").click(fun2=function(){
				initstep0($(this).attr("id"));		
				subjectname=$(this).html();
				//alert(subjectname);
				
		});

		function initstep0(id){
			if(stepstate==0){
				stepstate=1;//展开
				$("#iv-step0 li").css("z-index" ,1);
				var left=0;
				var top=0;
				var count=0;
				$("#iv-step0 li").each(function(){
					if(count==3){
						top+=190;
						left=0;	
						$(this).animate({left:"+="+left+""}).wait().animate({top:"+="+top+""});
	
						left+=270;
					}
					else{
						$(this).animate({left:"+="+left+""}).wait().animate({top:"+="+top+""});
	
						left+=270;
					}
					count++;
					$(".quickwords").css({display:"none"});
					
					$("#showbox").html("");		
					$("#matrix").html("");	
					$("#boxinfo").css({display : "none"});
					KW.length=0;
					TESTS.length=0;
					MARKPAD.length=0;
					subjectname="";
					$("#iv-step1").css({height:"0"}).hide();
					$("#iv-step2").css({height:"0"});
					$("#iv-step3").css({height:"0"});
				});
			}else{
				stepstate=0;
				$("#iv-step0 li").css("z-index" ,0);
				$("#"+id).css("z-index" ,1);
				$(".preloader").show();
				$("#iv-step0 li").animate({left:"10",top:"10"},200,function(){$("#iv-step1").css({height:"500"}).show(500);});
				//$(".preloader").hide(3000);
				$(".preloader").css({display : "none"});

				tempid=id;
				tempid=tempid.replace("type","qwords");
				//alert(tempid);
				$("#"+tempid+"").css({display:"block"});
				
			}
}
		
		$("#test").hide();
		$("#submit").click(function(){
			$("#showbox").append(" <div class=\"preword\"><div class=\"delspan\"></div><span>"+$("#keywordinput").val()+"</span></div>");
			$(".delspan").bind("click",function(){$(this).parent().replaceWith("");showTestScale();});
			showTestScale();
			$("#keywordinput").focus();
			$("#keywordinput").select();
			//$(".preword").bind(each,function(){$("#resultbox").append($(this).attr(value));});
		});
		$("#replay").click(function(){
			initstep0(0);
			/*
			$("#showbox").html("");		
			$("#matrix").html("");	
			$("#boxinfo").css({display : "none"});
			KW.length=0;
			TESTS.length=0;
			MARKPAD.length=0;
			$("#iv-step3").animate({height:0});
			$("#iv-step1").animate({height:400});*/
			
		});
		$("#confirm").click(function(){
			$(".preloader").show();
	 		$("#iv-step1").animate({height:0});
			$("#iv-step2").animate({height:250},500,function(){$(".preloader").hide()});
			//$("#iv-step1").css({display : "none"});
			//$("#iv-step2").css({display : "block"});
			var n=$(".preword").size();  //矩阵规模
			initArray(MARKPAD,n+1);//初始化矩阵
		
			//doCalculate(MARKPAD,n);
			var i=0;
			$(".preword").each(function(){
				KW[i]=$(this).text();
				i++;
			});
			makeTest(KW,TESTS);
			var myDate = new Date();
			doTest(TESTS,0,MARKPAD,KW,myDate.getTime());
			
		});
		$(".qword").click(function(){
			$("#showbox").append(" <div class=\"preword\" ><div class=\"delspan\"></div><span>"+$(this).text()+"</span></div>");
			showTestScale();
			$(".delspan").bind("click",function(){$(this).parent().replaceWith("");showTestScale();});
		});
		
		$("#calculate").click(function(){
			printArray(MARKPAD,"打分0"); 
			doCalculate(MARKPAD);
			outPut(MARKPAD,KW,subjectname);	
		});
		$("#keywordinput").keypress(function(event){
			if(event.which==13){
			$("#submit").trigger("click");
			
			}
		})
	});


// JavaScript Document
//打印数组矩阵
	function printArray(X,comment){
		$("#matrix").append( "<br />");
		$("#matrix").append( "<font color='red'>注释： "+comment+"</font><br />");
		var count=0;
		for(i=0;i<X.length;i++){
			for(j=0;j<X[i].length;j++){
				$("#matrix").append( X[i][j].toFixed(2)+" ");
				}
			$("#matrix").append( "<br />");
        }
		$("#matrix").append( "<br />");
	}
	function initArray(X,n){
		for(i=0;i<n;i++){X[i] = new Array();for(j=0;j<n;j++){X[i][j] =0;}}
		for(i=0;i<n;i++){X[i][i]=1; }
		
	}
	function markingArray(MARKPAD,x,y,printNum){
			MARKPAD[x][y]=parseInt(printNum);
			MARKPAD[y][x]=1/parseInt(printNum);

	}
//对输入好的矩阵进行计算
	function doCalculate(MARKPAD){
		//列汇总
		n=MARKPAD.length-1;
		for(i=0;i<n;i++){
			sum=0;
			for(j=0;j<n;j++){
				sum=sum+MARKPAD[j][i];
			}
			MARKPAD[n][i]=sum;
		}	
		//除数
		/*
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				MARKPAD[j][i]=MARKPAD[j][i]/MARKPAD[n][i];
			}	
		}*/
		
		//行汇总
		for(i=0;i<n;i++){
			sum=0;
			for(j=0;j<n;j++){
				sum=sum+MARKPAD[i][j]/MARKPAD[n][j];	
			}
			MARKPAD[i][n]=sum;
		}
		//验证
		sum=0;
		for(i=0;i<n;i++){
			sum=MARKPAD[i][n]+sum;
		}
		MARKPAD[n][n]=sum;
		//printArray(MARKPAD,"验证角"); //打印矩阵
	}
	function endStep2(MARKPAD,KW){
		
		$("#test").html("<a href=\"#\"  id=\"output\"  class=\"buttonbig\">查看结果</a>");
		$("#iv-step2").hide().show(1000);
		$("#output").bind("click",function(){outPut(MARKPAD,KW);});
		}
	
	function outPut(MARKPAD,KW){
		function sortNumberList(arr){
			for(var i=0;i<arr.length-1;i++){
				for(var j =(i+1);j<arr.length;j++){
					if(parseFloat(arr[i][1])<parseFloat(arr[j][1])){
						tempc=arr[i][0];
						tempn=arr[i][1];
						arr[i][0]=arr[j][0];
						arr[i][1]=arr[j][1];
						arr[j][0]=tempc;
						arr[j][1]=tempn;
						}
					}
				}
			//return arr;
		}
		
		n=KW.length;
		$(".preloader").show();
		$("#iv-step2").animate({height:0});
		$("#iv-step3").animate({height:(60+40*(n+1))},500,function(){$(".preloader").hide();});
		tempsubjectname=subjectname.replace("h3","span");
		//alert("<div>"+this.tempsubjectname+" 的测试结果：</div>");
		
		$("#matrix").html("<div>"+this.tempsubjectname+" <span>的测试结果：</span></div>");

		var arr =new Array();
		var arr2 =new Array();
		for(i=0;i<n;i++){
			arr[i]=new Array();
			value=(MARKPAD[i][n]/MARKPAD[n][n]*100).toFixed(2);
			arr[i][0]=KW[i];
			arr[i][1]=value;
		}	
		sortNumberList(arr);
		numsum=0;
		for(i=0;i<n;i++){
			numsum+=parseFloat(arr[i][1]);
			if(numsum>50){
				$("#matrix").append("<li style='background-position:"+(arr[i][1]/100*295-320)+"px'>["+arr[i][0]+"] "+arr[i][1]+"%   ---- 以上"+(i+1)+"项累计占比达到"+numsum.toPrecision(4)+"%</li>");
				numsum=-100;
			}else{
				$("#matrix").append("<li style='background-position:"+(arr[i][1]/100*295-320)+"px'>["+arr[i][0]+"] "+arr[i][1]+"% </li>");
				}
			}
		//$("#matrix").append(getpie());
	}
	
	function geTestHtml(groupname,x,y,value,name,keyno)
	{
		var HIGH=new Array();
		HIGH[1]="margin-top: 45px;";
		HIGH[3]="margin-top: 35px;";
		HIGH[5]="margin-top: 25px;";
		HIGH[7]="margin-top: 15px;";
		HIGH[9]="margin-top: 5px;";
		return "<div class=\""+groupname+"\" style=\""+HIGH[value]+"\" value=\""+value+"\" attr1=\""+x+"\" attr2=\""+y+"\"  keyno=\""+keyno+"\">"+name+"</div>"
	}
	function showTestScale(){
		
		$("#testscale").html(getTestScale());
		$("#boxinfo").css({display : "block"});
	}
	function getTestScale(){
		
		n=$(".preword").size();
		times=((n*n)/2-n/2);
		minute=((n*n)/2-n/2)*3;
		
		rehtml="";
		rehtml+= "您选择了 "+n+" 个关键词，";
		rehtml+="将会生成 "+times+" 次对比。";

		
		
		/*
		rehtml+="<br />";
		rehtml+="对比时间 约"+Math.floor(minute/60)+"分"+minute%60+"秒";
		*/
		return rehtml;
	}
	function makeTest(KW,TESTS){
		n=KW.length;
		idindex=0;
		for(i=0;i<n-1;i++){
			for(j=i+1;j<n;j++){
			cname="markbar";
			htmlstr="<div class='stepinfo'><div class='stepnow' style='width:"+((idindex+1)/((n*n)/2-n/2))*100+"%'></div><div class='stepcontent'>"+(idindex+1)+"/"+((n*n)/2-n/2)+"</div></div>";
			htmlstr+="<div class=\"contentmark\">"
			+geTestHtml(cname,i,j,9,"绝对重要","F")
			+geTestHtml(cname,i,j,7,"明显重要","F")
			+geTestHtml(cname,i,j,5,"相当重要","F")
			+geTestHtml(cname,i,j,3,"略微重要","F")
			+geTestHtml(cname,i,j,1,"相同","V")
			+geTestHtml(cname,j,i,3,"略微重要","S")
			+geTestHtml(cname,j,i,5,"相当重要","S")
			+geTestHtml(cname,j,i,7,"明显重要","S")
			+geTestHtml(cname,j,i,9,"绝对重要","S")
			+" </div><div class=\"titlemark\"><div class=\"title\" id=\"keyword-F\">"+KW[i]+"</div><div class=\"titlevs\"> VS</div><div class=\"title\" id=\"keyword-S\">"+KW[j]+ "</div></div>";

			//alert(htmlstr);
			TESTS[idindex]=htmlstr;
			//alert("idindex"+idindex+""+i+""+j);	
			idindex++;
			}
		}
		
	}
	function doTest(TESTS,n,MARKPAD,KW,starttime){
		var mydate = new Date();
		nowtime=mydate.getTime();
		uesdtime=nowtime-starttime;
		mlen=KW.length;
		times=(mlen*mlen/2)-mlen/2;
		pertime=uesdtime/(n+1);
		//alert("pertime"+pertime);
		alltime=pertime*(times);
		losttime=pertime*(times-n);
		//alert("losttime"+losttime);
		$("#test").animate({height:0});
		
		//$("#test").fadeOut();
		$("#test").html("<div class='timeinfo'> 平均一道题的时间 "+(pertime/1000).toFixed(2)+"秒  预计剩余"+Math.floor(losttime/60000)+"分"+(Math.floor(losttime/1000))%60 + "秒</div>");
		$("#test").append(TESTS[n]);
		$("#test").animate({height:280});
		$("#test").fadeIn();
		//alert(n+""+(TESTS.length-1))
		if(n==(TESTS.length-1)){
			$(".markbar").bind("click",function(){
				markingArray(MARKPAD,$(this).attr("attr1"),$(this).attr("attr2"),$(this).attr("value"));
				doCalculate(MARKPAD);
				endStep2(MARKPAD,KW);
				
			});
		} else {
				$(".markbar").bind("click",function(){
					markingArray(MARKPAD,$(this).attr("attr1"),$(this).attr("attr2"),$(this).attr("value"));
					doTest(TESTS,(n+1),MARKPAD,KW,starttime);
				})
		}
		
		$(".markbar").bind("mouseover",
			function(){
				var COLOR=new Array();
				COLOR[1]="#FFFF00";
				COLOR[3]="#FFBE00";
				COLOR[5]="#FF5800";
				COLOR[7]="#FE2C00";
				COLOR[9]="#AF0005";
				
				var COLOR2=new Array();
				COLOR2[1]="#E5FFAA";
				COLOR2[3]="#CAFF51";
				COLOR2[5]="#83FF00";
				COLOR2[7]="#4BC400";
				COLOR2[9]="#266800";
				$(this).css("background-color",COLOR[$(this).attr("value")]);
				if($(this).attr("keyno")=="F"){
					$("#keyword-F").css("background-color",COLOR[$(this).attr("value")]);
					$("#keyword-S").css("background-color",COLOR2[$(this).attr("value")]);
				}else if ($(this).attr("keyno")=="S"){
					$("#keyword-S").css("background-color",COLOR[$(this).attr("value")]);
					$("#keyword-F").css("background-color",COLOR2[$(this).attr("value")]);
				}else{
					$("#keyword-S").css("background-color",COLOR[$(this).attr("value")]);
					$("#keyword-F").css("background-color",COLOR[$(this).attr("value")]);
				}
			});
		
		$(".markbar").bind("mouseout",
			function(){
				$(this).css("background-color","#FFFFFF");
				$("#keyword-F").css("background-color","none");
				$("#keyword-S").css("background-color","none");
				}
			);
		
		
	}
	